// internal/planner/expression/expressions.go
package expression

import (
	"NumberBase/internal/planner/types"
	"fmt"
)

// ColumnRef 表示列引用
type ColumnRef struct {
	Column *types.Column
}

func (c *ColumnRef) ResultType() types.DataType {
	return c.Column.DataType
}

func (c *ColumnRef) String() string {
	if c.Column.Table != "" {
		return fmt.Sprintf("%s.%s", c.Column.Table, c.Column.Name)
	}
	return c.Column.Name
}

// Literal 表示字面量
type Literal struct {
	Value    string
	DataType types.DataType
}

func (l *Literal) ResultType() types.DataType {
	return l.DataType
}

func (l *Literal) String() string {
	if l.DataType == types.TypeString {
		return fmt.Sprintf("'%s'", l.Value)
	}
	return l.Value
}

// BinaryOp 表示二元操作
type BinaryOp struct {
	Left     types.Expression
	Right    types.Expression
	Op       types.OpType
	DataType types.DataType
}

func (b *BinaryOp) ResultType() types.DataType {
	return b.DataType
}

func (b *BinaryOp) String() string {
	opStr := opToString(b.Op)
	return fmt.Sprintf("(%s %s %s)", b.Left.String(), opStr, b.Right.String())
}

// 辅助函数：操作符转字符串
func opToString(op types.OpType) string {
	switch op {
	case types.OpEqual:
		return "="
	case types.OpNotEqual:
		return "<>"
	case types.OpLess:
		return "<"
	case types.OpLessEqual:
		return "<="
	case types.OpGreater:
		return ">"
	case types.OpGreaterEqual:
		return ">="
	case types.OpAnd:
		return "AND"
	case types.OpOr:
		return "OR"
	case types.OpNot:
		return "NOT"
	case types.OpAdd:
		return "+"
	case types.OpSub:
		return "-"
	case types.OpMul:
		return "*"
	case types.OpDiv:
		return "/"
	}
	return "UNKNOWN"
}
